package com.devilvan.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class SequenceFileReaderNewVersion {
    public static void sequenceFileReaderExpansionDemo() {
        SequenceFile.Reader reader = null;
        try {
            String uri = "hdfs://node01:8020/writeSequenceFileExpansion";
            Configuration conf = new Configuration();
            FileSystem.get(new URI(uri), conf, "Devilvan");
            Path path = new Path(uri);
            SequenceFile.Reader.Option file = SequenceFile.Reader.file(path);
            reader = new SequenceFile.Reader(conf, file);
            Writable keyWritable = ReflectionUtils.newInstance(MD5Hash.class, conf);
            Writable valueWritable = (Writable) ReflectionUtils.newInstance(Text.class, conf);
            long position = reader.getPosition();
            while (reader.next(keyWritable, valueWritable)) {
                String syncSeen = reader.syncSeen() ? "*" : "";
                System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen, keyWritable, valueWritable);
                //移动到下一个record开头的位置
                position = reader.getPosition(); // beginning of next record
            }
        } catch (IOException | InterruptedException | URISyntaxException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(reader);
        }
    }

    private static void sequenceFileReaderEasyDemo() {
        SequenceFile.Reader reader = null;
        try {
            //要读的SequenceFile
            String uri = "hdfs://node01:8020/writeSequenceFile";
            Configuration conf = new Configuration();
            Path path = new Path(uri);
            //读取SequenceFile的Reader的路径选项
            SequenceFile.Reader.Option pathOption = SequenceFile.Reader.file(path);
            //实例化Reader对象
            reader = new SequenceFile.Reader(conf, pathOption);
            //根据反射，求出key类型对象
            Writable key = (Writable)
                    ReflectionUtils.newInstance(reader.getKeyClass(), conf);
            //根据反射，求出value类型对象
            Writable value = (Writable)
                    ReflectionUtils.newInstance(reader.getValueClass(), conf);
            long position = reader.getPosition();
            System.out.println(position);
            while (reader.next(key, value)) {
                String syncSeen = reader.syncSeen() ? "*" : "";
                System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen, key, value);
                //移动到下一个record开头的位置
                position = reader.getPosition(); // beginning of next record
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(reader);
        }
    }

    public static void main(String[] args) {
        sequenceFileReaderEasyDemo();
//        sequenceFileReaderExpansionDemo();
    }
}
